local xinqun = fk.CreateSkill{
  name = "lvshi__xinqun",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable{
  ["lvshi__xinqun"] = "新裙",
  [":lvshi__xinqun"] = "锁定技，奇数轮次结束时，你回复1点体力；{当你受到伤害后，你可观看牌堆顶三张牌，然后获得其中花色数量唯一最多的牌，删除{}内容直到此轮结束。}",
  [":lvshi__xinqun_fix"] = "奇数轮次结束时，你回复1点体力。",

  ["$lvshi__xinqun1"] = "珠宫贝阙，一如当年。",
  ["$lvshi__xinqun2"] = "还会有人，在今日说起我的故事吗？",
}

local function get_most_frequent_suit(ids)
  local suit_counts = {
    ["heart"] = 0,
    ["diamond"] = 0,
    ["club"] = 0,
    ["spade"] = 0
  }
  for _, id in ipairs(ids) do
    local suit = Fk:getCardById(id):getSuitString()
    suit_counts[suit] = (suit_counts[suit] or 0) + 1
  end
  local max_count = 0
  local most_frequent_suit = nil
  for suit, count in pairs(suit_counts) do
      if count > max_count then
        max_count = count
        most_frequent_suit = suit
      end
  end
  local cards = table.filter(ids, function (id)
    return Fk:getCardById(id):getSuitString() == most_frequent_suit
  end)
  return cards
end

xinqun:addEffect(fk.Damaged, {
  anim_type = "masochism",
  dynamic_desc = function(self, player)
    if player:getMark("xinqun-round") > 0 then
      return "lvshi__xinqun_fix"
    end
    return "lvshi__xinqun"
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(xinqun.name) and player:getMark("xinqun-round") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local ids = room:getNCards(3)
    room:viewCards(player, {
      cards = ids,
      skill_name = xinqun.name,
    })
    local result = get_most_frequent_suit(ids)
    if #result > 1 then
      room:obtainCard(player, result, false, fk.ReasonJustMove, player, xinqun.name)
      room:addPlayerMark(player, "xinqun-round")
    end
  end,
})

xinqun:addEffect(fk.RoundEnd, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(xinqun.name) and player.room:getBanner("RoundCount") % 2 == 1 and player:isWounded()
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = xinqun.name,
    })
  end,
})



return xinqun
